<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>规则参数收集</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 50%;
            margin: 50px auto;
        }
        label {
            display: block;
            margin-bottom: 10px;
        }
        input[type="text"], input[type="number"], textarea {
            width: 10%;
            padding: 8px;
            margin-top: 5px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            float: right;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>规则参数收集表单</h2>
    <form id="ruleForm">
        <label for="rule_id">规则ID：</label>
        <input type="text" id="rule_id" name="rule_id" required>

        <label for="static_conditions">静态画像条件：</label>
        <div id="static_conditions_container"></div>
        <button type="button" id="addStaticCondition">添加静态条件</button>

        <label for="dynamic_cnt_conditions">动态计数条件：</label>
        <div id="dynamic_cnt_conditions_container"></div>
        <button type="button" id="addDynamicCntCondition">添加动态计数条件</button>

        <label for="dynamic_seq_condition">动态序列条件：</label>
        <div id="dynamic_seq_condition_container">
            <label>运算符：<input type="text" id="sequence_operator" name="sequence_operator" required></label>
            <input type="text" id="dynamic_seq_condition" name="dynamic_seq_condition" required>
        </div>
        <button type="button" id="addDynamicSeqCondition">添加动态序列条件</button>

        <label for="trigger_event_id">触发事件ID：</label>
        <input type="text" id="trigger_event_id" name="trigger_event_id" required>

        <input type="submit" value="提交" onclick="submitFormData()">
    </form>
</div>

<script>
    function submitFormData() {
        var jsonData = {};
        jsonData.rule_id = $("#rule_id").val();
        jsonData.static_condition = [];
        $("#static_conditions_container div").each(function() {
            var condition = {
                tag_name: $(this).find("input[name='tag_name']").val(),
                compare_type: $(this).find("input[name='compare_type']").val(),
                compare_value: $(this).find("input[name='compare_value']").val().split(',')
            };
            jsonData.static_condition.push(condition);
        });
        jsonData.dynamic_cnt_condition = [];
        $("#dynamic_cnt_conditions_container div").each(function() {
            var condition = {
                event_id: $(this).find("input[name='event_id']").val(),
                compare_type: $(this).find("input[name='compare_type']").val(),
                compare_value: parseInt($(this).find("input[name='compare_value']").val())
            };
            jsonData.dynamic_cnt_condition.push(condition);
        });
        jsonData.dynamic_seq_condition = {
            sequence_operator: $("#sequence_operator").val(),
            dynamic_seq_condition: $("#dynamic_seq_condition").val().split(',')
        };
        jsonData.trigger_event_id = $("#trigger_event_id").val();


        $.ajax({
            url: "http://localhost:8080/abc",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify(jsonData),
            success: function(response) {
                console.log("成功发送数据到后台");
                // 可以在这里处理后台响应
            },
            error: function(xhr, status, error) {
                console.error("发送数据到后台时出错:", error);
                // 可以在这里处理错误情况
            }
        });



    }

    $(document).ready(function() {
        $("#addStaticCondition").click(function() {
            $("#static_conditions_container").append('<div><label>标签名：<input type="text" name="tag_name" required></label>' +
                '<label>运算符：<input type="text" name="compare_type" required></label>' +
                '<label>目标值：<input type="text" name="compare_value" required></label></div>');
        });

        $("#addDynamicCntCondition").click(function() {
            $("#dynamic_cnt_conditions_container").append('<div><label>事件ID：<input type="text" name="event_id" required></label>' +
                '<label>运算符：<input type="text" name="compare_type" required></label>' +
                '<label>目标值：<input type="number" name="compare_value" required></label></div>');
        });

        $("#addDynamicSeqCondition").click(function() {
            $("#dynamic_seq_condition_container").append('<div><label>动态序列条件：</label>' +
                '<label>事件序列：<input type="text" name="dynamic_seq_condition" required></label>' +
                '<label>运算符：<input type="text" name="sequence_operator" required></label>' +
                '<label>目标值：<input type="number" name="compare_value" required></label></div>');
        });
    });
</script>

</body>
</html>
